package demo;

public class MyLinkedList implements IList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    /**
     * 头插
     * @param data
     */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if(head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    /**
     * 尾插
     * @param data
     */
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if(head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    /**
     * 任意位置插入
     * @param index
     * @param data
     */
    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()) {
            throw new IndexException("双链表中 下标不合法");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);

        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;

        while(index != 0) {
            cur = cur.next;
            index--;
        }

        return cur;
    }
    /**
     * 查找元素
     * @param key
     * @return
     */
    @Override
    public boolean contains(int key) {
        ListNode cur = head;

        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    /**
     * 删除元素
     * @param key
     *      1.遍历
     *      2.删除节点为头
     *      3.删除节点为尾
     *      4.删除节点在中间
     */
    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {//为头节点
                    head = head.next;
                    if (cur.next == null) {
                        last = null;
                    } else {
                        head.prev = null;
                    }
//                }else {//中间节点 尾节点
//                    if(cur.next == null) {//尾巴
//                        last = last.prev;
//                        last.next = null;
//                    }else {
//                        cur.prev.next = cur.next;
//                        cur.next.prev = cur.prev;
//                    }
//                }
                    }else if(cur.next == null) {//皆可
                        last = last.prev;
                        last.next = null;
                    }else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                return;
            }
            cur = cur.next;
        }
    }

    /**
     * 删除链表中所有key元素
     * @param key
     */
    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {//为头节点
                    head = head.next;
                    if (cur.next == null) {
                        last = null;
                    } else {
                        head.prev = null;
                    }
//                }else {//中间节点 尾节点
//                    if(cur.next == null) {//尾巴
//                        last = last.prev;
//                        last.next = null;
//                    }else {
//                        cur.prev.next = cur.next;
//                        cur.next.prev = cur.prev;
//                    }
//                }
                }else if(cur.next == null) {//皆可
                    last = last.prev;
                    last.next = null;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    /**
     * 求链表长度
     * @return
     */
    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;

        while(cur != null) {
            count++;
            cur = cur.next;
        }

        return count;
    }

    /**
     * 打印链表
     */
    @Override
    public void display() {
        ListNode cur = head;

        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 回收链表
     */
    @Override
    public void clear() {
        this.head = null;
        this.last = null;
    }
}
